package taikai;

import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;
import javax.swing.table.AbstractTableModel;
import static taikai.Grade.*;

public class PeopleData extends AbstractTableModel implements TeamsOrPlayers {

    private ArrayList<Person> people = new ArrayList<Person>();
    private final Class<?>[] types = new Class<?>[]{String.class, String.class, Grade.class, String.class, Team.class};
    private DefaultListModel teamMembers = new DefaultListModel();
    private boolean teamEvent = true;

    PeopleData() {
        people.add(new Person("Jeff", "Martin", SANDAN, "Hizen"));
        people.add(new Person("Paul", "Gray", SANDAN, "Hizen"));
        people.add(new Person("Stewart", "Daniels", NIDAN, "Hizen"));
        people.add(new Person("Alex", "Burch", YONDAN, "Hizen"));
        people.add(new Person("Clayon", "Stewart", SANDAN, "Hizen"));
        people.add(new Person("Gerardus", "Rukas", SANDAN, "Hizen"));
        people.add(new Person("Antonin", "Vachet", SANDAN, "Hizen"));
        people.add(new Person("Philip", "Wilson", SANDAN, "Hizen"));
        people.add(new Person("Natasha", "Nogic", SANDAN, "Hizen"));
        people.add(new Person("Chi", "Fung", IKKYU, "Hizen"));
        people.add(new Person("Dillon", "Lin", NIDAN, "Hizen"));
        people.add(new Person("Adam", "Pettifer", NIDAN, "Hizen"));
        people.add(new Person("Nick", "Davey", SANDAN, "Hizen"));
        people.add(new Person("Adam", "Tennant", SANDAN, "Hizen"));
        people.add(new Person("Gurdev", "Benipal", MUKYU, "Queen Mary"));
        people.add(new Person("Vai", "Hoang", MUKYU, ""));
        people.add(new Person("Richard", "Lee", MUKYU, ""));
        people.add(new Person("Aurelien", "Nacrour", MUKYU, ""));
        people.add(new Person("Barry", "Rutter", MUKYU, "Wakaba"));
        people.add(new Person("Susan", "Rutter", MUKYU, "Wakaba"));
        people.add(new Person("Matt", "Jackson", MUKYU, ""));
        people.add(new Person("Justin", "Ruddle", MUKYU, ""));
        people.add(new Person("Mark", "Doyle", MUKYU, ""));
        people.add(new Person("Eliand", "Murray", MUKYU, ""));
        people.add(new Person("Michael", "Tucker", MUKYU, ""));
        people.add(new Person("Frank", "Helbick", MUKYU, ""));
        people.add(new Person("Christain", "Metzger", MUKYU, ""));
        people.add(new Person("Clive", "McNaught", MUKYU, ""));
        people.add(new Person("Linda", "Ling", MUKYU, ""));
        people.add(new Person("Luc", "Borot", MUKYU, ""));
        people.add(new Person("Paul", "Wood", MUKYU, ""));
        people.add(new Person("David", "Spacey", MUKYU, ""));
        people.add(new Person("Armando", "Alemdar", MUKYU, "Hizen"));
        people.add(new Person("Atsuko", "Hosono", SHODAN, "Hizen"));
        people.add(new Person("Bernd", "Rose", NIDAN, "Hizen"));
        people.add(new Person("Bourak", "", MUKYU, "Hizen"));
        people.add(new Person("Charles", "Calix", MUKYU, "Hizen"));
        people.add(new Person("David", "Jordan", MUKYU, "Hizen"));
        people.add(new Person("Emma", "Hynes", MUKYU, "Hizen"));
        people.add(new Person("Endre", "Szvetnik", MUKYU, "Hizen"));
        people.add(new Person("Eva", "Kollarova", MUKYU, "Hizen"));
        people.add(new Person("Ingo", "Wickenhauser", SANDAN, "Hizen"));
        people.add(new Person("Julian", "Biddulph", NIDAN, "Hizen"));
        people.add(new Person("Kyeong-Seon", "Cha", NIDAN, "Hizen"));
        people.add(new Person("Lewis", "Guneratne", NIDAN, "Hizen"));
        people.add(new Person("Minaxi", "Arjan", MUKYU, "Hizen"));
        people.add(new Person("Misuk", "Bae", NIDAN, "Hizen"));
        people.add(new Person("Narendra", "Arjan", MUKYU, "Hizen"));
        people.add(new Person("Peter", "Tornkvist", SANDAN, "Hizen"));
        people.add(new Person("Pierre", "Toureau", MUKYU, "Hizen"));
        people.add(new Person("Russell", "Harding", MUKYU, "Hizen"));
        people.add(new Person("Ruth", "McDonald", SHODAN, "Hizen"));
        people.add(new Person("Satoshi", "Miyamura", GODAN, "Hizen"));
        people.add(new Person("Yukiko", "Miyamura", YONDAN, "Hizen"));
        people.add(new Person("Steve", "McDonald", NIDAN, "Hizen"));
        people.add(new Person("Tom", "Wolfe", MUKYU, "Hizen"));

        teamMembers.addElement("One");
        teamMembers.addElement("Two");
        teamMembers.addElement("Three");
    }

    @Override
    public int getRowCount() {
        return people.size();
    }

    @Override
    public int getColumnCount() {
        return teamEvent ? Person.Field.values().length : Person.Field.values().length - 1;
    }

    @Override
    public String getColumnName(int col) {
        return Person.Field.values()[col].title;
    }

    @Override
    public Class<?> getColumnClass(int i) {
        return types[i];
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        return true;
    }

    @Override
    public Object getValueAt(int row, int col) {
        final Person person = people.get(row);
        switch (col) {
            case 0:
                return person.getFirstName();
            case 1:
                return person.getLastName();
            case 2:
                return person.getGrade();
            case 3:
                return person.getDojo();
            case 4:
                return person.getTeam();
        }
        return "xxx";
    }

    @Override
    public void setValueAt(Object o, int row, int col) {
        switch (col) {
            case 0:
                people.get(row).setFirstName((String) o);
                break;
            case 1:
                people.get(row).setLastName((String) o);
                break;
            case 2:
                people.get(row).setGrade((Grade) o);
                break;
            case 3:
                people.get(row).setDojo((String) o);
                break;
        }
    }

    public ListModel getPeople() {
        return new AbstractListModel() {

            @Override
            public int getSize() {
                return people.size();
            }

            @Override
            public Object getElementAt(int i) {
                return people.get(i).getLastName() + " " + people.get(i).getFirstName();
            }
        };
    }

    public Person getPerson(int i) {
        return people.get(i);
    }

    void setTeamEvent(boolean teamEvent) {
        this.teamEvent = teamEvent;
        fireTableStructureChanged();
    }

    @Override
    public TeamOrPlayer getTeamOrPlayer(int i) {
        return getPerson(i);
    }

    @Override
    public int getSize() {
        return getRowCount();
    }

    void load(TaikaiFile taikaiFile) {
        people.clear();
        for (String[] data : taikaiFile.people) {
            people.add(new Person(data[0], data[1], Grade.valueOf(data[2]), optional(data, 3)));
        }
        setTeamEvent(taikaiFile.teamEvent);
    }

    private String optional(String[] data, int i) {

        if (i >= data.length) {
            return null;
        }

        return data[i];
    }
}
